<template>
  <div>
    <!-- Header 区域 -->
    <van-nav-bar
      fixed
      title="文章详情"
      left-arrow
      @click-left="$router.back()"
    />
    <!-- 文章加载等待中loading  -->
    <van-loading type="spinner" color="#1989fa" vertical v-if="Object.keys(essayDetail).length===0">疯狂加载中...</van-loading>
    <div v-else>
     <!-- 文章信息区域 -->
    <div class="article-container">
      <!-- 文章标题 -->
      <h1 class="art-title">{{ essayDetail?.title }}</h1>

      <!-- 用户信息 -->
      <van-cell
        center
        :title="essayDetail?.aut_name"
        :label="timeAgo(essayDetail?.pubdate)"
      >
        <template #icon>
          <img :src="essayDetail?.aut_photo" alt="" class="avatar" />
        </template>
        <template #default>
          <div>
            <van-button
              type="info"
              size="mini"
              v-if="essayDetail?.is_followed"
              @click="attention('true')"
              >已关注</van-button
            >
            <van-button
              icon="plus"
              type="info"
              size="mini"
              plain
              v-else
              @click="attention('flase')"
              >关注</van-button
            >
          </div>
        </template>
      </van-cell>

      <!-- 分割线 -->
      <van-divider></van-divider>

      <!-- 文章内容 -->
      <div class="art-content" v-html="essayDetail?.content"></div>

      <!-- 分割线 -->
      <van-divider>End</van-divider>

      <!-- 点赞 attitude -1无态度  0不喜欢  1 点赞-->
      <div class="like-box">
        <van-button
          icon="good-job"
          type="danger"
          size="small"
          v-if="essayDetail?.attitude === -1"
          @click="givelike('true')"
          >已点赞</van-button
        >
        <van-button
          icon="good-job-o"
          type="danger"
          plain
          size="small"
          v-else
          @click="givelike('false')"
          >点赞</van-button
        >
      </div>
    </div>
    <!-- 评论列表 -->
    <comment-list :artId="essayDetail?.art_id" />
    </div>
  </div>
</template>

<script>
import {
  detailAPI,
  followedUserAPI,
  unfollowedUserAPI,
  articleDisLikeAPI,
  articleLikeAPI
} from '@/api/index'
import { timeAgo } from '@/utils/day'
import { Toast } from 'vant'
import commentList from '@/views/ArticleDetail/commentList.vue'
let timer = null
export default {
  name: 'ArticeDetail',
  data () {
    return {
      essayDetail: {}
    }
  },
  created () {
    this.getDetail()
  },
  components: {
    commentList
  },
  methods: {
    // 防抖
    famhdou (callback) {
      clearTimeout(timer)
      timer = setTimeout(() => {
        if (callback) {
          callback()
        }
      }, 500)
    },

    timeAgo,
    // 1.0 获取文章详情
    async getDetail () {
      await detailAPI({ id: this.$route.query.art_id })
        .then((res) => {
          if (res.status !== 200) return Toast.fail(res.data.message)
          const {
            data: { data }
          } = res
          data && (this.essayDetail = data) // a&&(b=a)  a存在则赋值b,否则b还是原值
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 2.0  关注  取消关注
    attention (type) {
      if (type === 'true') {
        this.famhdou(this.unfollowed)
      } else {
        this.famhdou(this.followed)
      }
    },
    // 2.1 取关
    async unfollowed () {
      await unfollowedUserAPI({ uid: this.essayDetail.aut_id }).then((res) => {
        console.log(res)
        if (res.status === 204) {
          Toast.success('取关成功')
          this.essayDetail.is_followed = false
        } else {
          Toast.fail('取关失败')
        }
      })
    },
    // 2.2 关注
    async followed () {
      await followedUserAPI({ target: this.essayDetail.aut_id }).then((res) => {
        if (res.data.message !== 'OK') return Toast.fail('关注失败')
        this.essayDetail.is_followed = true
        Toast.success('关注成功')
      })
    },
    // 3.0 点赞或者取消点赞
    givelike (type) {
      if (type === 'true') {
        // true已点赞，做的就是取消点赞
        this.famhdou(this.articleDisLike)
      } else {
        this.famhdou(this.articleLike)
      }
    },
    // 3.1  取消点赞
    async articleDisLike () {
      await articleDisLikeAPI({ artId: this.essayDetail.art_id }).then(
        (res) => {
          if (res.status !== 204) return Toast.fail('取消失败')
          Toast.success('取消成功')
          this.essayDetail.attitude = 0
        }
      )
    },
    // 3.2  点赞
    async articleLike () {
      await articleLikeAPI({ target: this.essayDetail.art_id }).then((res) => {
        console.log(res)
        if (res.status !== 201) return Toast.fail('点赞失败')
        Toast.success('点赞成功')
        this.essayDetail.attitude = -1
      })
    }
  }
}
</script>

<style scoped lang="less">
.article-container {
  padding: 10px;
  margin-top: 46px;
}
/deep/ .van-nav-bar__title {
  color: #fff;
}
/deep/ .van-nav-bar__content {
  background: #007bff;
}
/deep/ .van-icon {
  color: #fff;
}
.art-title {
  font-size: 16px;
  font-weight: bold;
  margin: 10px 0;
}

.art-content {
  font-size: 12px;
  line-height: 24px;
  width: 100%;
  overflow-x: scroll;
  word-break: break-all;
  /deep/ img {
    width: 100%;
  }
  /deep/ pre {
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}

.van-cell {
  padding: 5px 0;
  &::after {
    display: none;
  }
}

.avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background-color: #f8f8f8;
  margin-right: 5px;
  border: none;
}

.like-box {
  display: flex;
  justify-content: center;
}

/deep/ .van-loading--vertical {
margin: 50px 0 0 0;
}
/deep/ .van-loading__text{
margin: 0 ;

}
</style>
